package gui.utils;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JTextField;

public class TextFieldMaxChar extends JTextField {

	private class TextFieldMaxCharKeyTyped implements KeyListener {

		private TextFieldMaxChar tfmc;

		public TextFieldMaxCharKeyTyped(TextFieldMaxChar tfmc) {
			this.tfmc = tfmc;
		}

		@Override
		public void keyPressed(KeyEvent e) {
		}

		@Override
		public void keyReleased(KeyEvent e) {
		}

		@Override
		public void keyTyped(KeyEvent e) {
			if (tfmc.getText().length() >= maxChar)
				e.consume();
		}

	}

	private static final long serialVersionUID = 1L;

	private int maxChar = 45;

	public TextFieldMaxChar() {
		super();
		this.addKeyListener(new TextFieldMaxCharKeyTyped(this));
	}

	public TextFieldMaxChar(int maxChar) {
		super();
		this.maxChar = maxChar;
		this.addKeyListener(new TextFieldMaxCharKeyTyped(this));
	}
}
